package controller.login;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Client;
import controller.dao.Dao_Client;


public class LogClient extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher rd = request.getRequestDispatcher("/client/login.jsp");
		rd.forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String log = request.getParameter("name");
	    String pass = request.getParameter("pass");
	    Dao_Client dao_cli = new Dao_Client();
	    Client cli = dao_cli.logClient(log, pass);
	    if(cli != null){
			HttpSession logClient = request.getSession();
			logClient.setAttribute("shopCart", new HashMap<String,Integer>());
			logClient.setAttribute("client", cli);
			response.getWriter().print("1");
		}
		else{
			response.getWriter().print("0");
		}
	}

}
